package org.osami.gateway.watteco;

import java.io.IOException;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.osami.gateway.sensor.data.SenseNode;
import org.osami.gateway.watteco.interfaces.IWattecoSmartplugIPv6;
import org.osgi.service.device.Constants;

@Component
@Provides
public class WattecoSmartplug implements IWattecoSmartplugIPv6
{

    public final static int MATCH_SERIAL = 10;
    public final static int MATCH_TYPE = 5;

    public final static String POWER_KEY = "Power";

    private boolean state;

    @ServiceProperty(name = Constants.DEVICE_CATEGORY,
                     value = "{Watteco}")
    private String[] categories;

    @ServiceProperty(name = Constants.DEVICE_SERIAL,
                     mandatory = true)
    private String serial;

    @ServiceProperty(name = "device.type",
                     value = "wattecospipv6")
    private String type;

    @ServiceProperty(value = SenseNode.TOPIC_PREFIX + "wattecosp")
    private String topic;


    public void noDriverFound() {
        System.out.println("WattecoSmartplug.noDriverFound()");
    }

    public void switchOn() throws IOException
    {
        System.out.println("switch on: "+serial);
        state = true;
    }

    public void switchOff() throws IOException
    {
        System.out.println("switch off: "+serial);
        state = false;
    }

    public void toggleSwitch() throws IOException
    {
        System.out.println("toggle: "+serial);
        state = !state;
    }

    public int getSwitchState() throws IOException
    {
        return state ? 1 : 0;
    }

    public void setSwitchState(boolean state) throws IOException
    {
        if (state) {
            switchOn();
        }
        else {
            switchOff();
        }
    }

    public void startMeasurements() throws IOException
    {
        
    }

    public int getMeasurement() throws IOException
    {
        int value = 0;
        if(state) {
            value = 40;
        }
        return value;
    }

    public String getID()
    {
        return serial;
    }
}
